package top.infopub.mgr.bpa.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.EffectSureAttDao;
import top.infopub.mgr.bpa.dao.EffectSureDao;
import top.infopub.mgr.bpa.dao.EffectSureDetailDao;
import top.infopub.mgr.bpa.domain.EffectSureDomain;
import top.infopub.mgr.bpa.service.EffectSureService;
import top.infopub.mgr.bpa.vo.EffectSureVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.sts.domain.StsEffectDomain;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.bpa.EffectSure;
import top.infopub.model.bpa.EffectSureAtt;
import top.infopub.model.bpa.EffectSureDetail;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class EffectSureServiceImpl extends GenericServiceImpl<EffectSure, String> implements
		EffectSureService {

	@Autowired
	private EffectSureDao effectSureDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;

    @Autowired
    private EffectSureDetailDao effectSureDetailDao;
    
    @Autowired
    private EffectSureAttDao effectSureAttDao;
	
	@Override
	public GenericDao<EffectSure, String> getDao() {
		return effectSureDao;
	}

    @Override
    public EffectSure selectLatestEffectSureByProCode(String proCode) {
        return effectSureDao.selectLatestEffectSureByProCode(proCode);
    }

    @Override
    public Page<EffectSureDomain> selectPageByDomainWhere(Page<EffectSureDomain> page,
                                                          EffectSureVo effectSureVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<EffectSureDomain>)effectSureDao.selectByDomainWhere(effectSureVo);
    }

    @Override
    public Page<EffectSureDomain> selectMyCheckByDomainWhere(Page<EffectSureDomain> page,
                                                             EffectSureVo effectSureVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<EffectSureDomain>)effectSureDao.selectMyCheckByDomainWhere(effectSureVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(EffectSureVo effectSure, List<EffectSureDetail> effectSureDetails,
                     List<EffectSureAtt> effectSureAtts) {
        //插入记录
        effectSureDao.insertById(effectSure);

        //批量插入子项
        if(CollectionUtils.isNotEmpty(effectSureDetails)){
            effectSureDetailDao.insertByBatch(effectSureDetails);
        }
        
        //批量插入附件
        if(CollectionUtils.isNotEmpty(effectSureAtts)){
            effectSureAttDao.insertByBatch(effectSureAtts);
        }
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.BPA_EFFECT_SURE;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, effectSure.getId(), effectSure.getProcessId(), null);
        
    }

    @Override
    public EffectSureVo queryById(String id) {
        return effectSureDao.queryById(id);
    }

    @Override
    public BigDecimal selectTotalAmount(EffectSureVo effectSureVo) {
        return effectSureDao.selectTotalAmount(effectSureVo);
    }

    @Override
    public Page<StsEffectDomain> selectStsByDomainWhere(Page<StsEffectDomain> page, String type,
                                                        String orgId, String proName, String year) throws ParseException {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Map<String, Object> where = new HashMap<String, Object>();
        where.put("proName", proName);
        if(StringUtils.isNotEmpty(year)){
            where.put("belongsYear", Integer.parseInt(year));
        }
        if(type.equals("2")){//区域公司
            where.put("hrOrgAreaId", orgId);
        }else if(type.equals("3")){//项目公司
            where.put("hrOrgProId", orgId);
        }
        String monthStartDate = year +"-01-01";
        String monthEndDate = year +"-12-31";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        where.put("monthStartDate", sdf.parse(monthStartDate));
        Date stsMonthEnd = DateUtils.getMonthEnd(sdf.parse(monthEndDate));
        where.put("monthEndDate", stsMonthEnd);
        List<StsEffectDomain> effectDomains = effectSureDao.selectStsByDomainWhere(where).getResult();
        for(StsEffectDomain effectDomain : effectDomains){
            String monthAndAmount = effectDomain.getMonthAmount();
            if(StringUtils.isNotEmpty(monthAndAmount)){
                String[] monthAmountArr = monthAndAmount.split(",");
                for(int i=0; i< monthAmountArr.length; i++){
                    String[] monthAmount = monthAmountArr[i].split("=");
                    String[] typeMonth = monthAmount[0].split("-");
                    int month = Integer.parseInt(typeMonth[1]);
                    BigDecimal amount = new BigDecimal(monthAmount[1]);
                    if(typeMonth[0].equals("1")){//创效计划
                        switch(month){
                            case 1:
                                effectDomain.setMonthPlanAmount1(amount);
                                break;
                            case 2:
                                effectDomain.setMonthPlanAmount2(amount);
                                break;
                            case 3:
                                effectDomain.setMonthPlanAmount3(amount);
                                break;
                            case 4:
                                effectDomain.setMonthPlanAmount4(amount);
                                break;
                            case 5:
                                effectDomain.setMonthPlanAmount5(amount);
                                break;
                            case 6:
                                effectDomain.setMonthPlanAmount6(amount);
                                break;
                            case 7:
                                effectDomain.setMonthPlanAmount7(amount);
                                break;
                            case 8:
                                effectDomain.setMonthPlanAmount8(amount);
                                break;
                            case 9:
                                effectDomain.setMonthPlanAmount9(amount);
                                break;
                            case 10:
                                effectDomain.setMonthPlanAmount10(amount);
                                break;
                            case 11:
                                effectDomain.setMonthPlanAmount11(amount);
                                break;
                            case 12:
                                effectDomain.setMonthPlanAmount12(amount);
                                break;
                        }
                    }else{//创效确认
                        switch(month){
                            case 1:
                                effectDomain.setMonthSureAmount1(amount);
                                break;
                            case 2:
                                effectDomain.setMonthSureAmount2(amount);
                                break;
                            case 3:
                                effectDomain.setMonthSureAmount3(amount);
                                break;
                            case 4:
                                effectDomain.setMonthSureAmount4(amount);
                                break;
                            case 5:
                                effectDomain.setMonthSureAmount5(amount);
                                break;
                            case 6:
                                effectDomain.setMonthSureAmount6(amount);
                                break;
                            case 7:
                                effectDomain.setMonthSureAmount7(amount);
                                break;
                            case 8:
                                effectDomain.setMonthSureAmount8(amount);
                                break;
                            case 9:
                                effectDomain.setMonthSureAmount9(amount);
                                break;
                            case 10:
                                effectDomain.setMonthSureAmount10(amount);
                                break;
                            case 11:
                                effectDomain.setMonthSureAmount11(amount);
                                break;
                            case 12:
                                effectDomain.setMonthSureAmount12(amount);
                                break;
                        }
                    }
                }
            }
        }
        return (Page<StsEffectDomain>)effectDomains;
    }
}